package com.dnf.crawler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.dnf.bean.Goods;
import com.dnf.util.DateUtil;

/**
 * @author Administrator
 *
 */
public class GoodsPriceCrawler {
public static Document getDoc(String url)
{
	Document doc = new Document(url);
	try {
		doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2595.400 QQBrowser/9.6.10872.400")
				.ignoreContentType(true).timeout(10*1000).get();
	} catch (IOException e) {
		System.out.println(e.getMessage());
	}
	return doc;
}
public static Elements getTableRows(Document doc)
{
	Elements tableRows = doc.select("#txt_html table:eq(1) tr:gt(0)");
	return tableRows;
}
public  static Goods getGoods(Element tableRow,String goodsName)
{
    Element date = tableRow.select("td:eq(0)").get(0);
	Element price = tableRow.select("td:eq(1)").get(0);
	String goodsDate = DateUtil.formateDate(date.text().toString());
	int goodsPrice = Integer.parseInt(price.text().toString());
	Goods goods = new Goods(goodsName,goodsDate,goodsPrice);
	return goods;	
}
public  static List<Goods> getGoodsList(Document doc,String goodsName)
{
	Elements tableRows = getTableRows(doc);
	List<Goods> goodsList = new ArrayList<>();
	for(Element tr:tableRows)
	{
		goodsList.add(getGoods(tr,goodsName));
	}
	return goodsList;
}
public static void main(String[] args) {
	String url = "http://dnf.yxwujia.com/search/search_right_proprice1.aspx?name=%C3%AC%B6%DC%B5%C4%BD%E1%BE%A7%CC%E5&areaname=%bf%e74%a3%a8%ba%cf%c7%f8%ba%f3%d0%c2%ca%fd%be%dd%a3%a9&serversname=%bf%e74&cxpjjx=&cxgjx=&qtckx=&firstday=2017%2F4%2F19%200%3A00%3A00%20%20%20%20&unit=yxb&cxdjx=djx&ID=2817&rnd=0.0683096751764023&cxzqx=rx&lastday=2018%2F4%2F19%200%3A00%3A00%20%20%20%20&mode=txtlist&serversid=000300330001&areaid=00030033&op=modify";
	Document doc = getDoc(url);
	System.out.println(doc);
	/*List<Goods> goodsList = getGoodsList(doc);
	for(Goods goods:goodsList)
		System.out.println(goods);*/
}
}
